package com.dmap.run.controller.api;

import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import com.dmap.run.bo.RunRecordSimple;
import com.dmap.run.constants.RunConstans;
import com.dmap.run.controller.api.param.RunRecordParam;
import com.dmap.run.po.RunRecord;
import com.dmap.run.po.mongo.MapTrack;
import com.dmap.run.service.MapTrackService;
import com.dmap.run.service.RunRecordService;
import com.dmap.run.vo.RunRecordDetailVo;
import com.dmap.run.vo.RunRecordHistoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * \* User: silent
 * \* Date: 16/05/18 Time: 16:46
 * \* Description: 跑相关接口
 * \
 */
@Api(value = "跑步相关接口", description = "跑步相关接口")
@Controller
@RequestMapping(value = "/api/dmap-run-server/v1/run")
public class RunRecordController {
    @Resource
    private RunRecordService runRecordService;
    @Resource
    private MapTrackService mapTrackService;

    @ResponseBody
    @ApiOperation(value = "接受跑步数据", notes = "接受跑步数据")
    @RequestMapping(value = "/accept/run/data", method = RequestMethod.POST)
    public BaseVo accpectRunRecord(@RequestBody RunRecordParam param) {
        BaseVo baseVo = new BaseVo();
        if (param.getUserId() == null || param.getType() == null
                || param.getDataSource() == null || param.getStartTime() == null
                || param.getEndTime() == null || param.getDistanceTotal() == null
                || param.getStride() == null || param.getCalorie() == null
                || param.getStepNumTotal() == null || param.getTimeTotal() == null) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        if (runRecordService.isExistRunRecord(param.getUserId(), param.getStartTime(), param.getEndTime())) {
            baseVo.setCode(RunConstans.RESPONSE_IS_EXIST_THIS_RECORD_CODE);
            baseVo.setMsg(RunConstans.RESPONSE_IS_EXIST_THIS_RECORD_MSG);
            return baseVo;
        }
        boolean isSuccess = runRecordService.insertRunRecord(param);
        if (!isSuccess) {
            baseVo.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
            return baseVo;
        }
        if (CollectionUtils.isNotEmpty(param.getTrackList())) {
            MapTrack mapTrack = new MapTrack();
            mapTrack.setUserId(param.getUserId());
            mapTrack.setType(1);
            mapTrack.setDataSource(param.getDataSource());
            mapTrack.setStartTime(param.getStartTime());
            mapTrack.setEndTime(param.getEndTime());
            mapTrack.setTrackList(param.getTrackList());
            mapTrackService.insertMapTrack(mapTrack);
        }
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "获取用户历史跑步总公里数", notes = "获取用户历史跑步总公里数")
    @RequestMapping(value = "/get/user/total/distance", method = RequestMethod.GET)
    public BaseVo getUserDistanceTotal(@RequestParam Long userId) {
        BaseVo baseVo = new BaseVo();
        if (userId == null) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        int totalDistance = runRecordService.selectUserTotalDistance(userId);
        Map<String, Integer> map = new HashMap<>();
        map.put("totalDistance", totalDistance);
        baseVo.setData(map);
        return baseVo;
    }


    @ResponseBody
    @ApiOperation(value = "获取历史跑步数据列表", notes = "获取历史跑步数据列表")
    @RequestMapping(value = "/get/run/data", method = RequestMethod.GET)
    public BaseVo getRunRecordList(@RequestParam Long userId, @RequestParam Integer pageIndex, @RequestParam Integer pageSize) {
        BaseVo baseVo = new BaseVo();
        if (userId == null || pageIndex == null || pageSize == null) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        RunRecordHistoryVo runRecordHistoryVo = runRecordService.selectRunRecordHistoryList(userId);
        if (runRecordHistoryVo.getRunNum() > 0) {
            List<RunRecord> runRecordList = runRecordService.selectRunRecordListByPage(userId, pageIndex, pageSize);
            List<RunRecordSimple> runRecordSimpleList = new ArrayList<>(pageSize);
            for (RunRecord record : runRecordList) {
                //处理ios错误卡路里数据
                if (record.getDistanceTotal() > 0 && (record.getCalorie() / record.getDistanceTotal()) * 1000 > 150) {
                    record.setCalorie(record.getCalorie() / 1000);
                }
                runRecordSimpleList.add(new RunRecordSimple(record));
            }
            runRecordHistoryVo.setRunRecordSimpleList(runRecordSimpleList);
        }
        baseVo.setData(runRecordHistoryVo);
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "获取跑步数据详情", notes = "获取跑步数据详情")
    @RequestMapping(value = "/accept/run/data/detail", method = RequestMethod.GET)
    public BaseVo getRunRecordDetail(@RequestParam Long runRecordId) {
        BaseVo baseVo = new BaseVo();
        if (runRecordId == null) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        RunRecord runRecord = runRecordService.selectRunRecordById(runRecordId);
        //没有查到返回错误码
        if (runRecord == null) {
            baseVo.setCode(RunConstans.RESPONSE_NOT_EXIST_THIS_RECORD_CODE);
            baseVo.setMsg(RunConstans.RESPONSE_NOT_EXIST_THIS_RECORD_MSG);
            return baseVo;
        }
        //处理ios错误卡路里数据
        if (runRecord.getDistanceTotal() > 0 && (runRecord.getCalorie() / runRecord.getDistanceTotal()) * 1000 > 150) {
            runRecord.setCalorie(runRecord.getCalorie() / 1000);
        }
        RunRecordDetailVo runRecordDetailVo = new RunRecordDetailVo(runRecord);
        //获取地图轨迹数据
        MapTrack mapTrack = mapTrackService.selectMapTrackByUserIdAndStartTimeAndEndTime(runRecord.getUserId(), runRecord.getStartTime(), runRecord.getEndTime());
        if (mapTrack != null) {
            runRecordDetailVo.setTrackList(mapTrack.getTrackList());
        } else {
            //没有地图轨迹放回一个空集合
            runRecordDetailVo.setTrackList(Collections.emptyList());
        }
        baseVo.setData(runRecordDetailVo);
        return baseVo;
    }

}

